package org.abettor.pushbox.download;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.abettor.pushbox.model.MessageBean;
import org.abettor.pushbox.model.MessageBoxBean;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

public class ParaDownloadMsg {
	public static final int HASMORE = 1, NOTHASMORE = 0, ERROR = -1;
	public static final int SEND_SUCCESS = 1;
	/**
	 * 是否有更多的数据
	 * @param content
	 * @return
	 */
	public static  int isHasMoreMsgBox(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("h");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			return Integer.parseInt(value);
		} catch (Exception e) {
			Log.e("x", "y", e);
			return ERROR;
		}
	}
	/**
	 * 是否有更多的数据
	 * @param content
	 * @return
	 */
	public static int isHasMoreMsg(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("h");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			return Integer.parseInt(value);
		} catch (Exception e) {
			Log.e("x", "y", e);
			return ERROR;
		}
	}
	/**
	 * 解析消息的列表
	 * @param content
	 * @return
	 */
	public static List<MessageBean> paraMessage(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<MessageBean> retList = new ArrayList<MessageBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				MessageBean bean = new MessageBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("ni".equals(nodeName)) {
						bean.setNickName(tmp);
					} else if ("t".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setTime(date);
					} else if("c".equals(nodeName)){
						bean.setContent(tmp);
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
			return null;
		}
	}
	/**
	 * 解析结果
	 * @param content
	 * @return
	 */
	public static List<MessageBoxBean> paraMessageBox(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		List<MessageBoxBean> retList = new ArrayList<MessageBoxBean>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			for (int i = 0; i < list.getLength(); i++) {
				MessageBoxBean bean = new MessageBoxBean();
				Node node = list.item(i);
				NodeList nodes = node.getChildNodes();
				// 解析id
				int count = nodes.getLength();
				for (int j = 0; j < count; j++) {
					Node pairNode = nodes.item(j);
					String nodeName = pairNode.getNodeName();
					String tmp = pairNode.getFirstChild().getNodeValue();
					if ("id".equals(nodeName)) {
						bean.setId(Integer.parseInt(tmp));
					} else if ("ni".equals(nodeName)) {
						bean.setNickName(tmp);
					} else if ("t".equals(nodeName)) {
						Date date = sdf.parse(tmp);
						bean.setTime(date);
					} else if("tc".equals(nodeName)){
						bean.setTotalCount(Integer.parseInt(tmp));
					} else if("uc".equals(nodeName)){
						bean.setUnreadCount(Integer.parseInt(tmp));
					} else if("su".equals(nodeName)){
						bean.setSummary(tmp);
					}
				}
				retList.add(bean);
			}
			in.close();
			return retList;
		} catch (Exception e) {
			Log.e(ParaDownloadRetValue.class.getName(), "", e);
			return null;
		}
	}
	/**
	 * 添加评论的操作
	 * @param content
	 * @return
	 */
	public static boolean isSendMsgSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return (retValue==SEND_SUCCESS);
		}catch(Exception e){
			Log.e("error", "error", e);
			return false;
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
}
